package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IDomain;
import com.macasa.panel.entity.api.IPowerDnsServiceMaster;
import com.macasa.panel.entity.api.IPowerDnsServiceSlave;

@Entity
@Table(name = "power_dns_service_slave")
@Getter
@Setter
@ToString(callSuper = true)
public class PowerDnsServiceSlave extends PowerDnsService implements IPowerDnsServiceSlave {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long		serialVersionUID	= 9123414223206157384L;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PowerDnsServiceMaster.class)
	@JoinColumn(name = "fk_power_dns_service_master_id", nullable = false)
	private IPowerDnsServiceMaster	master;

	@OneToMany(mappedBy = "powerDnsServiceSlave", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Domain.class)
	@ForeignKey(name = "fk_domain_04")
	private List<IDomain>			domains;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PowerDnsServiceSlave)) {
			return false;
		}

		final PowerDnsServiceSlave other = CastHelpers.cast(obj, PowerDnsServiceSlave.class);

		return super.equals(obj) && Objects.equal(this.master, other.master) && Objects.equal(this.domains, other.domains);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.master, this.domains);
	}
}
